using UnityEngine;
using System.Collections;

[System.Serializable]
public class DespawnOnTrigger : MonoBehaviour {
	
	public UnityEngine.GameObject objectToDespawn;
	public bool onEnter = true;
	public string tagToTrigger = "Player";
	
	private bool triggered = false;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	
	
	void OnTriggerEnter(Collider other)  {
		if (!triggered && onEnter && other.tag == tagToTrigger)  {
			despawn();
		}
	}
	
	void OnTriggerExit(Collider other) {
		if (!triggered && !onEnter && other.tag == tagToTrigger) {
			despawn();
		}
	}
	
	private void despawn() {
		objectToDespawn.SetActive(false);
		triggered = true;
	}
}
